import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/log_util.dart';
import 'package:wallet_utils/report_widget/date_picker/base/brn_picker.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:wallet_utils/toast.dart';

/// @author kxc on 2022/1/5
/// @description

typedef OnSelectedDate = void Function(String date);
typedef OnCleanDate = void Function();

class StoreDatePicker extends StatefulWidget {
  StoreDatePicker(this.title,
      {Key? key,
      this.onSelectedDate,
      this.onCleanDat,
      this.startIndex = 10,
      this.endIndex = 18})
      : super(key: key);
  final String title;
  final OnSelectedDate? onSelectedDate;
  final OnCleanDate? onCleanDat;
  late int startIndex;
  late int endIndex;
  @override
  _StoreDatePickerState createState() => _StoreDatePickerState();
}

class _StoreDatePickerState extends State<StoreDatePicker> {
  late FixedExtentScrollController startExtentScrollController;
  late FixedExtentScrollController endExtentScrollController;
  List<String> startList = [];
  List<String> endList = [];
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: ScreenUtil().screenHeight * 0.5,
      child: Column(
        children: [
          _headerWidget(),
          Expanded(child: bodyWidget(context)),
          SizedBox(
            height: ScreenUtil().bottomBarHeight + 10.w,
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    startExtentScrollController =
        FixedExtentScrollController(initialItem: widget.startIndex);
    endExtentScrollController =
        FixedExtentScrollController(initialItem: widget.endIndex);
    _setupData();
  }

  @override
  void dispose() {
    startExtentScrollController.dispose();
    endExtentScrollController.dispose();
    super.dispose();
  }

  Widget _headerWidget() {
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w),
      height: 48.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          InkWell(
            child: Text(
              '取消',
              style: TextStyleUtils.style969799(fontSize: 14.sp),
            ),
            onTap: () {
              widget.onCleanDat!();
            },
          ),
          Text(
            widget.title,
            style: TextStyleUtils.style323233(
                fontSize: 16.sp, fontWeight: FontWeight.w500),
          ),
          InkWell(
            child: Text(
              '确定',
              style: TextStyleUtils.styleF65247(fontSize: 14.sp),
            ),
            onTap: () {
              String startTime = startList[widget.startIndex];
              String endTime = endList[widget.endIndex];
              if (int.parse(startTime.split(':')[0]) <
                  int.parse(endTime.split(':')[0])) {
                LogUtils.d(
                    '${int.parse(startTime.split(':')[0])}  ${int.parse(endTime.split(':')[0])}');

                widget.onSelectedDate!('$startTime-$endTime');
              } else {
                ToastUtils.showToast('开始日期不能大于结束日期');
              }
            },
          ),
        ],
      ),
      decoration: BoxDecoration(
        color: ColorManager.colorFFFFFF,
        border: Border(
            bottom: BorderSide(
                color: ColorManager.color000000.withOpacity(0.1), width: 0.5)),
      ),
    );
  }

  Widget bodyWidget(BuildContext context) {
    return Container(
      width: double.infinity,
      color: ColorManager.colorFFFFFF,
      child: Row(
        children: [
          Expanded(
            child: BrnPicker.builder(
                backgroundColor: ColorManager.colorFFFFFF,
                lineColor: ColorManager.colorEBEDF0,
                scrollController: startExtentScrollController,
                itemExtent: 48.w,
                useMagnifier: true,
                magnification: 1.2,
                onSelectedItemChanged: (index) {
                  widget.startIndex = index;
                  setState(() {});
                },
                childCount: startList.length,
                itemBuilder: (context, index) {
                  return Container(
                      alignment: Alignment.center,
                      child: Text(
                        startList[index],
                        style: index == widget.startIndex
                            ? TextStyleUtils.style323233(
                                fontWeight: FontWeight.w500)
                            : TextStyleUtils.style646566(),
                      ));
                }),
          ),
          Expanded(
            child: BrnPicker.builder(
                backgroundColor: ColorManager.colorFFFFFF,
                lineColor: ColorManager.colorEBEDF0,
                scrollController: endExtentScrollController,
                itemExtent: 48.w,
                useMagnifier: true,
                magnification: 1.2,
                onSelectedItemChanged: (index) {
                  widget.endIndex = index;
                  setState(() {});
                },
                childCount: endList.length,
                itemBuilder: (context, index) {
                  return Container(
                      alignment: Alignment.center,
                      child: Text(
                        endList[index],
                        style: index == widget.endIndex
                            ? TextStyleUtils.style323233(
                                fontWeight: FontWeight.w500)
                            : TextStyleUtils.style646566(),
                      ));
                }),
          ),
        ],
      ),
    );
  }

  ///初始化数据
  void _setupData() {
    for (int i = 0; i < 24; i++) {
      if (i < 10) {
        startList.add('0$i:00');
        endList.add('0$i:00');
      } else {
        startList.add('$i:00');
        endList.add('$i:00');
      }
    }
  }
}
